package com.zxy.salenet.web.web.controller;

import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.pojo.SalenetGoods;
import com.zxy.salenet.web.pojo.SalenetOrgin;
import com.zxy.salenet.web.pojo.SalenetType;
import com.zxy.salenet.web.pojo.dto.UpLoadRequestDTO;
import com.zxy.salenet.web.pojo.vo.GoodsTypeVO;
import com.zxy.salenet.web.service.AdminGoodsOperationService;
import com.zxy.salenet.web.service.UserOperationService;
import com.zxy.salenet.web.utils.FileUtil;
import com.zxy.salenet.web.utils.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author: zxy
 * @version: 1.0
 * @description:    管理员对于商品界面管理
 */
/**
 * AdminGoodsOperationController.
 */
@Api(value = "管理员对于商品管理API",tags = "管理员对于商品管理API")
@RestController
@RequestMapping(path = "/adminGoodsOperation")
public class AdminGoodsOperationController {
    @Autowired
    private AdminGoodsOperationService adminGoodsOperationService;

    @Autowired
    private UserOperationService userOperationService;


    @Resource
    HttpServletRequest request;

    //处理文件上传
    @ApiOperation(value = "上传图片",notes = "上传文件即可")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @RequestMapping(path = "/upFile",method = RequestMethod.POST)
    @ResponseBody
    public Json uploadImg(@RequestParam MultipartFile file) {
        Json json = new Json();
        String fileName = file.getOriginalFilename();
        //设置文件上传路径
        String filePath = "D:\\img\\";
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, fileName);
            json.setMessage("上传成功");
            json.setData(fileName);
            json.setCode(200);
            return json;
        } catch (Exception e) {
            json.setMessage("上传失败");
            json.setData("失败");
            json.setCode(800);
            return json;
        }
    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @ApiOperation(value = "添加一个商品",notes = "添加一条商品信息 goodsType是商品类型对应Type_Id json格式 {\"goodsName\":\"CHANEL香奈儿炫亮魅力丝绒唇膏狮子系列 口红58\",\n" +
            "    \"goodsType\":77,\n" +
            "    \"goodsPrice\":789.00,\n" +
            "    \"goodsSellingPrice\":1089.00,\n" +
            "    \"goodsOrgin\":\"法国\",\n" +
            "    \"goodsBrand\":7981,\n" +
            "    \"goodsTimes\":\"2020-12-09\",\n" +
            "    \"goodsCount\":1000,\n" +
            "    \"goodsImg\":\"/e:/imgs.png\",\n" +
            "    \"goodsDescribed\":\"500ml 能够提升气色\",\n" +
            "    \"goodsStatus\":0}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/addGoods")
    private Json addGoods(@RequestBody SalenetGoods goods){
        Json json = new Json();
        System.out.println(goods);
        Integer addResult = adminGoodsOperationService.addGoods(goods);
        if(addResult == 1){
            json.setCode(200);
            json.setData(1);
            json.setMessage("添加成功");
            return json;
        }
        json.setData(0);
        json.setCode(800);
        json.setMessage("添加失败");
        return json;
    }

    /**
     * 修改商品信息
     * @param goods
     * @return
     */
    @ApiOperation(value = "修改对应的商品信息",notes = "发送对应的商品修改的信息 {\n" +
            "    \"id\":1,\n" +
            "    \"goodsId\":10082,\n" +
            "    \"goodsName\":\"CHANEL香奈儿炫亮魅力丝绒唇膏狮子系列 口红58\",\n" +
            "    \"goodsType\":77,\n" +
            "    \"goodsPrice\":759.00,\n" +
            "    \"goodsSellingPrice\":1089.00,\n" +
            "    \"goodsOrgin\":\"法国\",\n" +
            "    \"goodsBrand\":7981,\n" +
            "    \"goodsTimes\":\"2020-12-09\",\n" +
            "    \"goodsCount\":1000,\n" +
            "    \"goodsImg\":\"/e:/imgs.png\",\n" +
            "    \"goodsDescribed\":\"500ml 能够提升气色\",\n" +
            "    \"goodsStatus\":0\n" +
            "}\n")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/updateGoods")
    private Json updateGoods(@RequestBody SalenetGoods goods){
        Json json = new Json();
        Integer updateResult = adminGoodsOperationService.updateGoods(goods);
        if(updateResult == 1){
            json.setCode(200);
            json.setData(1);
            json.setMessage("修改成功");
            return json;
        }
        json.setCode(800);
        json.setData(1);
        json.setMessage("修改失败");
        return json;
    }

    /**
     * 删除/下架商品信息
     * @return
     */
    @ApiOperation(value = "删除/下架商品信息",notes = "发送商品的goodsId给后端 JSON格式 {\"goodsId\":10076}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/deleteGoods")
    private Json deleteGoods(@RequestBody Map<String,Integer> map){
        Json json = new Json();
        Integer goodsId = map.get("goodsId");
        Integer deleteResult = adminGoodsOperationService.deleteGoodsByGoodsId(goodsId);
        if(deleteResult == 1){
            json.setData(1);
            json.setCode(200);
            json.setMessage("删除成功");
            return json;
        }
        json.setData(0);
        json.setCode(800);
        json.setMessage("删除失败");
        return json;
    }

    /**
     * 已下架商品重新上架商品
     * @return
     */
    @ApiOperation(value = "已下架商品重新上架商品",notes = "发送商品的goodsId给后端 JSON格式 {\"goodsId\":10076}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/putUpGoods")
    private Json putUpGoods(@RequestBody Map<String,Integer> map){
        Json json = new Json();
        Integer goodsId = map.get("goodsId");
        Integer deleteResult = adminGoodsOperationService.deleteGoodsByGoodsIdUp(goodsId);
        if(deleteResult == 1){
            json.setData(1);
            json.setCode(200);
            json.setMessage("重新上架成功");
            return json;
        }
        json.setData(0);
        json.setCode(800);
        json.setMessage("重新上架失败");
        return json;
    }

    /**
     * 获取所有的商品分类
     * @return
     */
    @ApiOperation(value = "获取所有的商品分类",notes = "无参数")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = false)
    @PostMapping("/queryAllGoodsType")
    private Json queryAllGoodsType(){
        Json json = new Json();
        List<GoodsTypeVO> list = adminGoodsOperationService.queryAllGoodsType();
        json.setData(list);
        json.setCode(200);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 获取父级商品分类
     * @return
     */
    @ApiOperation(value = "获取所有的父级商品分类",notes = "无需发送参数")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = false)
    @PostMapping(path = "/queryParentGoods")
    private Json queryParentGoodsType(){
        Json json = new Json();
        List<SalenetType> typeList = adminGoodsOperationService.queryParentGoodsType();
        json.setMessage("查询成功");
        json.setCode(200);
        json.setData(typeList);
        return json;
    }

    /**
     * 添加商品类型
     * @param salenetType
     * @return
     */
    @ApiOperation(value = "添加一条商品类型",notes = "发送一个商品类型数据 父级商品类型{\n" +
            "    \"typeName\":\"珠宝\",\n" +
            "    \"typeParentId\":0\n" +
            "},非父级类型{\n" +
            "    \"typeName\":\"项链\",\n" +
            "    \"typeParentId\":94\n" +
            "} typeParenId 是获取父级商品商品分类获取成下拉列表选择 0 代表没有父级")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/addGoodsType")
    private Json addGoodsType(@RequestBody SalenetType salenetType){
        Json json = new Json();
        Integer addResult = adminGoodsOperationService.addGoodsType(salenetType);
        if(addResult !=0){
            json.setCode(200);
            json.setData(1);
            json.setMessage("添加成功");
            return json;
        }
        json.setCode(900);
        json.setData(0);
        json.setMessage("添加失败");
        return json;
    }

    /**
     * 获取所有的商品信息
     * @param map
     * @return
     */
    @ApiOperation(value = "获取所有的商品信息",notes = "获取所有的商品信息  JSON格式数据 {\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam()
    @PostMapping(path = "/AdminQueryAllGoods")
    private Json AdminQueryAllGoods(@RequestBody Map<String,Integer> map){
        Integer pageNum = map.get("pageNum");
        Integer pageSize = map.get("pageSize");
        Json json = new Json();
        PageInfo<SalenetGoods> pageInfo = userOperationService.queryGoodsByName("", pageNum, pageSize);
        json.setCode(200);
        json.setData(pageInfo);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 根据商品分类获取商品信息
     *
     * @return
     */
    @ApiOperation(value = "根据商品分类获取对应的商品信息",notes = "根据商品类型获取商品信息 下拉列表中的value的值为商品类型的编号 如 口红对应的编号为 77,JSON格式:{\"goodsType\":77,\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/querySomeGoodsByType")
    private Json querySomeGoodsByType(@RequestBody Map<String, Integer> map) {
        Json json = new Json();
        Integer goodsType = map.get("goodsType");
        Integer pageNum = map.get("pageNum");
        Integer pageSize = map.get("pageSize");
        PageInfo<SalenetGoods> allGoods = userOperationService.queryGoodsByGoodsType(goodsType, pageNum, pageSize);
        json.setData(allGoods);
        json.setCode(200);
        json.setMessage("查询成功");
        return json;
    }


    /**
     * 添加商品品牌
     * @param orgin
     * @return
     */
    @ApiOperation(value = "添加一个商品的商品品牌信息",notes = "数据 JSON格式 {\"orginName\":\"香奈儿\",\n" +
            "\"orginComm\":\"香奈儿（Chanel）是法国奢侈品品牌，创始人是Coco Chanel（原名是Gabrielle Bonheur Chanel ，中文名是加布里埃·香奈儿），该品牌于1910年在法国创立。\"}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/addOrgin")
    private Json addOrgin(@RequestBody SalenetOrgin orgin){
        Json json = new Json();
        Integer addResult = adminGoodsOperationService.addOrgin(orgin);
        if(addResult == 0){
            json.setData(0);
            json.setCode(800);
            json.setMessage("添加失败");
            return json;
        }
        json.setData(1);
        json.setCode(200);
        json.setMessage("添加成功");
        return json;
    }

    /**
     * 根据编号删除品牌
     * @return
     */
    @ApiOperation(value = "发送品牌的 orgin_id",notes = "发送当前商品的orgin_Id JSON格式 {\"orginId\":7981}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/deleteOrgin")
    private Json deleteOrgin(@RequestBody Map<String,Integer> map){
        Json json = new Json();
        Integer orginId = map.get("orginId");
        Integer result = adminGoodsOperationService.deleteOrginById(orginId);
        if(result == 1){
            json.setCode(200);
            json.setData(1);
            json.setMessage("删除成功");
            return json;
        }
        json.setCode(800);
        json.setData(0);
        json.setMessage("删除错误");
        return json;
    }

    /**
     * 根据编号删除品牌
     * @return
     */
    @ApiOperation(value = "获取商品品牌",notes = "获取商品品牌 ")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/queryOrgin")
    private Json queryOrgin(){
        Json json = new Json();
        List<SalenetOrgin> orgins = adminGoodsOperationService.queryAllOrgin();
        if(orgins.size() > 0){
            json.setCode(200);
            json.setData(orgins);
            json.setMessage("获取成功");
            return json;
        }
        json.setCode(800);
        json.setData(0);
        json.setMessage("获取错误");
        return json;
    }
}
